package org.brunoc.searchstores4j.vo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.brunoc.searchstores4j.SupportedStores;
import org.brunoc.searchstores4j.util.HttpClientFactory;
import org.brunoc.searchstores4j.vo.models.IStoreObject;
import org.brunoc.searchstores4j.vo.models.Product;

import com.googlecode.inutils4j.MyStringUtils;

/**
 * Class that implements search via HyperText Transfer Protocol at Submarino.com.br
 * @author Bruno Candido Volpato da Cunha
 *
 */
public class SubmarinoStore extends IStoreObject {

	/**
	 * If you have a Submarino Affiliate ID, put here
	 */
	private static final Long AFFILIATED_ID = 999999L;
	
	/**
	 * Default constructor for SubmarinoStore
	 */
	public SubmarinoStore() {
		super();
		this.name = "Submarino";
		this.searchUrl = "http://www.submarino.com.br/busca?offset=[#OFFSET]&limit=[#LIMIT]&q=[#SEARCH]&x=0&dep=%20&y=0";
		this.store = SupportedStores.SUBMARINO;
	}

	private boolean supportLimit = true;

	/* (non-Javadoc)
	 * @see org.brunoc.searchstores4j.vo.models.IStoreObject#makeUrl(java.lang.String, int)
	 */
	public String makeUrl(String query, int offset) {
		String urlRet = searchUrl.replace("[#SEARCH]", query);
		urlRet = urlRet.replace("[#OFFSET]", String.valueOf(offset));

		if (supportLimit) {
			urlRet = urlRet.replace("[#LIMIT]", String.valueOf(20));
		}

		return urlRet;
	}

	/* (non-Javadoc)
	 * @see org.brunoc.searchstores4j.vo.models.IStoreObject#search(java.lang.String)
	 */
	@Override
	public List<Product> search(String url) throws HttpException, IOException {
		List<Product> productList = new ArrayList<Product>();

		String content = MyStringUtils.getContent(HttpClientFactory.getInstance(), url);

		content = content.split("<div class=\"productVitrine\">")[1].trim();
		content = content.split("<div class=\"bottomPageList\">")[0].trim();
		content = content.replaceAll("\t", " ").trim();
		content = content.replaceAll("  ", " ").trim();

		String[] productSplit = content.split("<li class=\"\">");

		for (String productStructure : productSplit) {
			if (!productStructure.contains(">por: <strong>"))
				continue;

			Product product = new Product(this.store);

			String name = MyStringUtils.regexFindFirst("<span class=\"name entry-title\"><strong>(.*)</strong></span>", productStructure);
			String brand = MyStringUtils.regexFindFirst("<span class=\"brand\"><strong>(.*)</strong></span>", productStructure);
			// String description = MyStringUtils.regexFindFirst("[\\f,\\n,\\r,\\W\\D\\d]+(<span class=\"description\">(.{0,})</span>)[\\f,\\n,\\r,\\W\\D\\d]+", productStructure);
			String description = MyStringUtils.regexFindFirst("<span class=\"description\">(.*)</span>", productStructure);
			String image = MyStringUtils.regexFindFirst("src=\" (.*)\" class=\"image\"", productStructure);
			String price = MyStringUtils.regexFindFirst("<span class=\"for\">por: <strong>(.*)</strong></span>", productStructure);
			String productUrl = MyStringUtils.regexFindFirst("[\\f,\\n,\\r,\\W\\D\\d]+(<a href=\"(.+)\" class=\"link\">)[\\f,\\n,\\r,\\W\\D\\d]+", productStructure, 2);

			product.setName(name);
			product.setBrand(brand);
			product.setDescription(description);
			product.setImageUrl(image);

			String priceReplace = price.replace("R$", "");
			priceReplace = priceReplace.replace(".", "");
			priceReplace = priceReplace.replace(",", ".");

			product.setPrice(Double.valueOf(priceReplace));
			product.setProductUrl(productUrl + "?franq=" + AFFILIATED_ID);

			productList.add(product);
		}

		return productList;
	}

}
